import { BaseURL } from './global'

import api from '../api/index.js'
class Request {
  // 静态成员
  static paramsObj = {}
  static token = ''
  static result = 'handleRusult'

  constructor(method, url, data, contentType, hasAuthorization, showError) {
    // 接收请求参数
    // method 请求方式
    // url 请求路径
    // data 请求参数
    // contentType MIME-Type
    // hasAuthorization 是否携带请求头
    // showError 是否提示错误信息
    this.paramsObj = {
      method,
      url,
      data,
      contentType,
      hasAuthorization,
      showError
    }
    this.handleRusult = this.handleRequest(this.paramsObj)
  }

  // 请求处理函数
  handleRequest({ method = 'GET', url = '', data = {}, contentType = 'json', hasAuthorization = true, showError = true }) {
    // 存放静态成员
    Request.paramsObj = {
      method: method.toUpperCase(),
      url: BaseURL + url,
      data,
      header: {}
    }
    // console.log(Request.paramsObj);

    Request.token = uni.getStorageSync('token') || ''
    return new Promise((resolve, reject) => {
      // 请求头配置
      if (contentType === 'queryString') {
        Request.paramsObj.header['Content-Type'] = 'application/x-www-form-urlencoded'
      } else if (contentType === 'json') {
        Request.paramsObj.header['Content-Type'] = 'application/json'
      } else if (contentType === 'formData') {
        Request.paramsObj.header['Content-Type'] = 'multipart/form-data; boundary=----WebKitFormBoundaryNSySzVc8GVeoPkBP'
        Request.paramsObj.header['Accept'] = 'application/json, text/javascript, */*; q=0.01'
      }
      if (Request.token && hasAuthorization) {
        Request.paramsObj.header['token'] = Request.token
      }
      // 加载状态
      uni.showLoading({
        title: '加载中..',
        mask: true
      })

      // 发送请求
      uni.request({
        ...Request.paramsObj,
        success: res => {
          if (!res.data) {
            uni.hideLoading()
            flag = true
            resolve(res)
            if (res.statusCode) {
              if (showError)
                this.showError({
                  data: {
                    code: res.statusCode
                  }
                })
            }
          } else {
            if (showError) this.showError(res)
            uni.hideLoading()
            resolve(res.data)
          }
        },
        fail: err => {
          if (showError) this.showError(err)
          uni.hideLoading()
          reject(err)
        }
      })
    })
  }

  async showError({ data: { code, msg } }) {
    switch (code) {
      case 0:
        uni.showToast({
          title: msg,
          icon: 'none',
          position: 'bottom',
          duration: 2000
        })
        break
      case 401:
        setTimeout(() => {
          uni.navigateTo({
            url: '/subPages/login/login'
          })
        }, 1000)
        uni.showToast({
          title: '请登陆',
          icon: 'none',
          position: 'bottom',
          duration: 2000
        })
        break
      case 500:
        uni.showToast({
          title: msg,
          icon: 'none',
          position: 'bottom',
          duration: 2000
        })
        break
      // case 0:
      // 	uni.showToast({
      // 		title: msg,
      // 		icon: 'none',
      // 		position: 'bottom',
      // 		duration: 1000
      // 	})
      // 	break
      default:
        break
    }
  }
}

export default (method, url, data, contentType, hasAuthorization, showError) => {
  return new Request(method, url, data, contentType, hasAuthorization, showError)[Request.result]
}
